package cn.stronglink.oil.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.stronglink.oil.MyApplication;
import cn.stronglink.oil.R;
import cn.stronglink.oil.ipset.IpSetActivity;
import cn.stronglink.oil.map.BaiduMapActivity;
import cn.stronglink.oil.map.webmap.LoadWebMapActivity;
import cn.stronglink.oil.widget.ExceptionHintDialog;
import cn.stronglink.oil.widget.ProgressDialog;

/**
 * Created by tgb on 2017/4/24 22:41
 */

public class LoginActivity extends AppCompatActivity implements ILoginView{
    private EditText et_password;
    private EditText et_userName;
    private ProgressDialog progressDialog;
    private ILoginPresenter loginPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loginPresenter = new LoginPresenter(this);
        initView();
    }

    private void initView() {
        et_password = (EditText) findViewById(R.id.et_password);
        et_userName = (EditText) findViewById(R.id.et_userName);
    }

    public void onLogin(View view) {
        if(this.getUserName().isEmpty()){
            Toast.makeText(this,"请输入用户名",Toast.LENGTH_LONG).show();
            return;
        }
        if(this.getPassword().isEmpty()){
            Toast.makeText(this,"请输入邀请码",Toast.LENGTH_LONG).show();
            return;
        }
        loginPresenter.login();
    }
    public void onSetIp(View view){
        this.startActivity(new Intent(LoginActivity.this, IpSetActivity.class));
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    @Override
    public String getPassword() {
        return et_password.getText().toString();
    }

    @Override
    public String getUserName() {
        return et_userName.getText().toString();
    }

    @Override
    public void loginSuccess(User user) {
        User.setInstance(user);
        startActivity(new Intent(LoginActivity.this, LoadWebMapActivity.class).putExtra("psw",et_password.getText().toString()));
    }

    @Override
    public void loginFail(Exception e) {
        final ExceptionHintDialog dialog = new ExceptionHintDialog(LoginActivity.this);
        dialog.setDialogMessage(e.getMessage()+"");
        dialog.setOnOkClickListener(new ExceptionHintDialog.OnOkClickListener() {
            @Override
            public void onOkClick() {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    public void showProgress(String msg) {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(getString(R.string.login_loading));
        progressDialog.show();
    }

    @Override
    public void hideProgerss() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }
}
